﻿using QunProject.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace QunProject.Manage.Controllers
{
    /// <summary>
    /// 平台用户
    /// </summary>
    public class MemberController : BaseController
    {
        // 首页
        public ActionResult Index()
        {
            return View();
        }
        // 编辑页
        public ActionResult Edit(int? id)
        {
            ViewBag.ID = id;
            return View();
        }
        // 获取数据
        public JsonResult AjaxData(int? memberId, int page = 1, int limit = 20, string qPhone = "", string qName = "", string qNumber = "")
        {
            var list = EF.member.Where(x => true);
            if (memberId.HasValue)
                list = list.Where(x => x.ID == memberId);
            if (!qPhone.IsNullOrWhiteSpace())
                list = list.Where(x => x.Phone.Contains(qPhone));
            if (!qName.IsNullOrWhiteSpace())
                list = list.Where(x => x.Name.Contains(qName));
            if (!qNumber.IsNullOrWhiteSpace())
                list = list.Where(x => x.Number.Contains(qNumber));

            var data = list.OrderByDescending(x => x.CreateTime).ToList().Skip((page - 1) * limit).Take(limit).Select(x => new
            {
                x.ID,
                x.Remark,
                x.Phone,
                x.Name,
                x.Introduce,
                x.Number,
                x.Img,
                x.Identity,
                x.IsAdmin,
                x.IsProve,
                x.States,
                x.BanReason,
                CreateTime = x.CreateTime.Value.ToString("yyyy-MM-dd hh:mm:ss"),
                BanTime = x.BanTime.HasValue ? x.BanTime.Value.ToString("yyyy-MM-dd hh:mm:ss") : "-"
            });

            return Json(Result.SuccessReturnData(list.Count(), data));
        }
        // 提交编辑
        public JsonResult AjaxEdit(Model.member model)
        {
            Model.member _model = new Model.member();
            if (model.ID > 0)
            {
                _model = EF.member.SingleOrDefault(x => x.ID == model.ID);
                _model = EF.member.Attach(_model);
                _model.IsAdmin = model.IsAdmin;
                _model.Identity = model.Identity;
                _model.Remark = model.Remark;
                _model.IsProve = model.IsProve;
            }
            else
            {
                if (model.Name.IsNullOrWhiteSpace())
                    return Json(Result.Fail("不允许添加用户"));
            }

            bool isSucc = false;
            if (model.ID > 0)
            {
                isSucc = EF.SaveChanges() > 0;
            }
            else
            {
                EF.member.Add(_model);
                isSucc = EF.SaveChanges() > 0;
            }
            return isSucc ? Json(Result.Success()) : Json(Result.Fail());
        }
        // 改变状态
        public JsonResult AjaxStates(int ID, int States, string BanReason = "")
        {
            var model = EF.member.SingleOrDefault(x => x.ID == ID);
            if (model == null)
                return Json(Result.Fail("不存在此用户"));

            model = EF.member.Attach(model);
            model.States = States;
            model.BanReason = BanReason;

            bool isSucc = EF.SaveChanges() > 0;
            return isSucc ? Json(Result.Success()) : Json(Result.Fail());
        }
        // 重置密码
        public JsonResult AjaxReload(int id)
        {
            var model = EF.member.SingleOrDefault(x => x.ID == id);
            if (model == null)
                return Json(Result.Fail("不存在此用户"));

            model = EF.member.Attach(model);
            model.Password = Common.Security.MD5Encrypt32(UserDefaultPassword);

            bool isSucc = EF.SaveChanges() > 0;
            return isSucc ? Json(Result.Success()) : Json(Result.Fail("密码未改变"));
        }
        // 查询实体
        public JsonResult AjaxEntity(int id)
        {
            var model = EF.member.SingleOrDefault(x => x.ID == id);
            if (model == null)
                return Json(Result.Fail("不存在此管理员"));
            return Json(Result.Success(model));
        }
    }
}